$(function(){
	//登录检验
var login_btn = $("#yonghuming");
var register_btn = $("#tuichu");

if(getCookie("true")=="true"){
	login_btn.text("您好！"+getCookie("user"));
	//login_btn.attr("href","javascript:;");
	register_btn.text("退出").css("color","#EE0011");
	register_btn.attr("href","javascript:;");	
}else{
	login_btn.text("登录").css("color","#EE0011").attr("href","denglu.html");
	register_btn.text("注册").css("color","#EE0011").attr("href","zhuce.html");	
}

	register_btn.click(function(){
	setCookie("true",false,setCookieDate(7),"/");
	login_btn.text("登录").css("color","#EE0011").attr("href","javascript:;");
	register_btn.text("注册").css("color","#EE0011").attr("href","javascript:;");
	login_btn.click(function(){
		window.location.href = "denglu.html";
	});
	register_btn.click(function(){
		window.location.href = "zhuce.html";
	})
	
})
//添加
				var cookies = document.cookie;
				//console.log(cookies);
				var cook = cookies.split("; ");
				var arr = [];
				for (var i = 0 ; i < cook.length; i++){
					var daid = cook[i].split("=");
					var nameid = daid[0]*1;
					if(!isNaN(nameid)){
						arr.push(daid[1]);
					}
				}
				//alert(arr);
				$.ajax({
					url:"../json/list.json",
					dataType:"json",
					async:false,
					success:function(data){
						var mag = data.data;
						var str = "";
						for(var i = 0; i < arr.length;i++){
							for(var j = 0; j < mag.length;j++){
								if(arr[i] == mag[j].id){
									//alert(1);
									str += '<div class="tjsp" need="'+mag[j].id+'"><input class="l one checkbox1" type="checkbox"   /><div class="l imgdiv"><img src="../img/list/'+mag[j].id+'.jpg"/></div><div class="l xiangqing"><a class="l" href="javascript:;">'+mag[j].atext+'</a>';
									str += '<span class="xinxi">颜色：<span class="color">灰色</span>&nbsp;&nbsp;尺码:<span class="size">'+getCookie(arr[i]+"s")+'</span></span><span class="l cuxiao"><span class="temai">特卖</span>数量有限，请尽快提交订单！</span></div><div class="l sela"><span class="l price">¥'+mag[j].price+'</span><span class="l sale">'+mag[j].sale+'</span>';
									str += '</div><div class="l shuliang"><a class="l left" href="javascript:;">-</a><input class="l" type="text" value="'+getCookie(arr[i]+"n")+'"/><a class="l right" href="javascript:;">+</a></div><div class="l jifen">'+Number(getCookie(arr[i]+"n"))*mag[j].price+'</div><div class="l delete"><a class="l shoucang"  href="javascript:;">移入收藏</a><a class="l delete" href="javascript:;">删除商品</a></div></div>';
								}
								$("#tjspall").html(str);
							}
						}
					}
				});
//判断是否为空
var kong = 0;
for(var i in arr){
	
	if (arr[i] < 200 ){
		kong++;
	}
	if( kong == 0){
		$(".kong").css("display","block");
		$("#tjspall").css("display","none");
		$("#content .nei .qxtopxia").css("display","none");
	}else{
		$(".kong").css("display","none");
		$("#tjspall").css("display","block");
		$("#content .nei .qxtopxia").css("display","block");
	}
}
//全选
suanqian();
//先让所有的都为对勾
//$(".checkbox1").prop("checked",true);
//$(".checkbox").prop("checked",true);
//算初始全部价格
$("#content .qxbottom .two .jiesuandiv input").click(function(){

	var xuanzhong = $(".checkbox1");
	for(var i = 0 ; i < xuanzhong.length; i++){
		if($(xuanzhong[i]).prop("checked") == true){
		var name = $(xuanzhong[i]).parent().attr("need");
			setCookie(name+"b",name)				
		}				
	}		
	suanqian();
	gouwudai();
	var firstmoney = $("#content .qxbottom .two .jsje strong").html();
	//alert(firstmoney)
	setCookie("firstmoney",firstmoney)
	if($("#content .qxbottom .two .jsje strong").html() == "¥0.00"){
		alert("亲！你还没有选取宝贝哦！")
	}else if(confirm("立即去结算？我就让你嘿嘿嘿")){
		window.location.href="dingdan.html"
	}
})

////选取的价格
//var money1 = $(".tjsp").children(".jifen");
//var firstmoney =0;
//for(var i = 0 ; i < money.length; i++){
//	firstmoney += Number($(money[i]).html());	
//}





	






$("#content .nei .qxtop span.th-chk input").click(function(){
	if($("#content .nei .qxtop span.th-chk input").prop("checked")==true){
		$(".checkbox").prop("checked",true);
		$(".checkbox1").prop("checked",true);
		suanqian();
		gouwudai();
	}else{
		$(".checkbox").prop("checked",false);
		$(".checkbox1").prop("checked",false);
		suanqian();
		gouwudai();
	}
	//$(input)
})
$("#content .qxbottom span.left input").click(function(){
	if($("#content .qxbottom span.left input").prop("checked")==true){
		$(".checkbox").prop("checked",true);
		$(".checkbox1").prop("checked",true);
		suanqian();
		gouwudai();
	}else{
		$(".checkbox").prop("checked",false);
		$(".checkbox1").prop("checked",false);
		suanqian();
		gouwudai();
	}
	//$(input)
})


$(".checkbox1").click(function(){
	if($(this).prop("checked")==false){
		$(".checkbox").prop("checked",false);
	}
		suanqian();
		gouwudai();
})

//收藏 删除
$("#content .nei .tjsp .delete a.delete").click(function(){
	var need = $(this).parent().parent().attr("need");
	removeCookie(need);
	removeCookie(need+"n");
	//setCookie(need+"n",0,setCookieDate(7));
	gouwudai();
})

$("#content .nei .tjsp .delete a.shoucang").click(function(){
	alert("收藏成功！亲！")
})

//全部删除
$("#content .qxbottom span.left a.scxz").click(function(){
	var checkbox1 = $(".checkbox1");
	for(var i = 0; i < checkbox1.length ;i++){
		if($(checkbox1[i]).prop("checked")== true){
			var need = $(checkbox1[i]).parent().attr("need");
			//alert(need)
			removeCookie(need);
			removeCookie(need+"n");
		}
	}
	setCookie("All",0,setCookieDate(7))
	window.location.reload();
	suanqian();
	gouwudai();
})










	//调整数量
	$("#content .nei .tjsp .shuliang a.left").click(function(){
		var need = $(this).parent().parent().attr("need");
		
		var num	= Number($(this).next().val()) - 1;
		if(num < 1 ){
			num = 1;
			alert("亲 最少数量为1哦！")
		}
		$(this).next().val(num);
		var thisprice = Number($(this).parent().prev().children(".price").html().substr(1,5));
		var zj = num*thisprice;
		$(this).parent().next().html(zj);
		setCookie(need+"n",num,setCookieDate(7))
		suanqian();
		gouwudai();
		//window.location.reload()
	})
	$("#content .nei .tjsp .shuliang a.right").click(function(){
		var need = $(this).parent().parent().attr("need");
		var num	= Number($(this).prev().val()) + 1;
		if(num > 99 ){
			num = 99;
			alert("亲 已达到最大数量了哦！")
		}
		$(this).prev().val(num);
		var thisprice = Number($(this).parent().prev().children(".price").html().substr(1,5));
		var zj = num*thisprice;
		$(this).parent().next().html(zj);
		setCookie(need+"n",num,setCookieDate(7))
		suanqian();
		gouwudai();
		//window.location.reload()
	})
	
	
	//金额
	function suanqian(){
	var money = $(".tjsp").children(".jifen");
	var firstmoney =0;
	for(var i = 0 ; i < money.length; i++){
		if($(money[i]).parent().children(".checkbox1").prop("checked") == true){
			
			firstmoney += Number($(money[i]).html());	
		}
		
	}
	$("#content .qxbottom .suanzhang span strong.yi").html("¥"+firstmoney+".00");
	$("#content .qxbottom .suanzhang span strong.si").html("¥"+firstmoney+".00");
	$("#content .qxbottom .two .jsje strong").html("¥"+firstmoney+".00");
}
	
	
	
	
	function gouwudai(){
		var All=0;
		for(var i = 0 ; i <200;i++){
			var a =  Number(getCookie(i+"n"));
			All += a;
		}
		setCookie("All",All,setCookieDate(7))	
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
})
